package com.meixiang.view.statusView;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutCompat;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.meixiang.R;
import com.meixiang.view.GifView;

/**
 *
 * 状态布局（无数据、无网络、正在加载中...）
 * author:xiejingwen
 */
public class StatusView extends LinearLayoutCompat {
    private View vLine;
    private LinearLayout llNoData;
    private ImageView ivEmotion;
    private TextView tvMessage;
    private Button btnConfirm;
    private Context mContext;

    private LinearLayout llLoading;
    private GifView gifView;
    public StatusView(Context context) {
        this(context, null);
    }

    public StatusView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public StatusView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        setOrientation(VERTICAL);
        initView();
    }

    private void initView() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.view_attach, null);
        view.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        view.setClickable(true);

        vLine=view.findViewById(R.id.view_line);
        llNoData= (LinearLayout) view.findViewById(R.id.ll_no_data);
        llLoading= (LinearLayout) view.findViewById(R.id.ll_loading);
        ivEmotion= (ImageView) view.findViewById(R.id.iv_emotion);
        tvMessage= (TextView) view.findViewById(R.id.tv_message);
        btnConfirm= (Button) view.findViewById(R.id.btn_confirm);
        gifView= (GifView) view.findViewById(R.id.gif_view);
        gifView.setMovieResource(R.raw.pageloading);
        addView(view);
        setVisibility(GONE);
    }
    /**
     * 修改背景颜色（默认 系统背景）
     */

    public void setBackgroundColor(int resId){
        setBackgroundResource(ContextCompat.getColor(mContext, resId));
    }
    /**
     * 修改顶部线条颜色（默认title_line）
     */
    public void setLineColor(int resId){
        vLine.setBackgroundResource(resId);
    }

    /**
     * 隐藏顶部线条
     */
    public void hintLineView(){
        vLine.setVisibility(GONE);
    }
    /**
     * 显示无网络状态
     */
    public void NoNewWork(OnClickListener listener){
        NoDataOrLoading(false);
        ivEmotion.setImageDrawable(ContextCompat.getDrawable(mContext, R.mipmap.emotion3));
        tvMessage.setText("连接出错，请检查网络后重试");
        btnConfirm.setOnClickListener(listener);
    }


    /**
     * 显示无数据、加载失败状态，有点击事件
     * @param message 显示的内容
     * @param btnText 按钮文字
     * @param listener 点击事件
     */
    public void NoData( String message,  String btnText, OnClickListener listener){
        NoDataOrLoading(false);
        ivEmotion.setImageDrawable(ContextCompat.getDrawable(mContext, R.mipmap.emotion2));
        tvMessage.setText(message);
        if (listener != null) {
            btnConfirm.setText(btnText);
            btnConfirm.setVisibility(VISIBLE);
            btnConfirm.setOnClickListener(listener);
        } else {
            btnConfirm.setVisibility(GONE);
        }
    }

    /**
     *显示无数据状态 无点击事件
     * @param message 显示的内容
     */
    public void NoData(String message){
        NoData(message,null,null);
    }


    /**
     * 显示正在加载中状态
     */
    public void Loading(){
        NoDataOrLoading(true);
    }

    /**
     * 显示‘加载中’布局还是‘无数据无网络’布局
     * @param isLoading true为加载中布局
     */
    protected void NoDataOrLoading(boolean isLoading){
        setVisibility(VISIBLE);
        if (isLoading){
            llNoData.setVisibility(GONE);
            llLoading.setVisibility(VISIBLE);
        }else{
            llNoData.setVisibility(VISIBLE);
            llLoading.setVisibility(GONE);
        }
    }

    /**
     * 隐藏布局（请求成功或网络连接正常后调用）
     */
    public void removeAttachView(){
        if (this.getParent()!=null){
            ViewGroup viewGroup= (ViewGroup) this.getParent();
            viewGroup.removeView(this);
        }
    }

}
